var sex = 0;
var app = angular.module('app', []);

app.controller('nbController', function($scope){
	$scope.nbname = [];
	for(var i=0; i<=31; i++){
		$scope.nbname.push(i);
	}

	$scope.ybcount = [1, 2, 3, 4, 5]
});

$(function() {
	$(".xb span:not(:first-child)").on('click',function(){
		$('.xb span').removeClass('hover');
		$(this).addClass('hover');
		sex = $(this).attr('v');
	});
	$('.yb').hide();
	$('.choose input').on('click', function(){
		$('.choose span').removeClass('hover');
		if($(this).is(':checked')){
			$('.choose span:last-child').addClass('hover');
			$('.nb').hide();
			$('.yb').show();
			return;
		}
		$('.choose span').first().addClass('hover'); 
		$('.nb').show();
		$('.yb').hide();
	});

	$('.yb div:nth-child(2) input').on('click', function(){
		var idx = $(this).index()+1;
		var z = $(".yb div:first-child span:nth-child("+idx+")"); 
		var f = $(".yb div:last-child span:nth-child("+idx+")"); 
		// console.log(z);
		if ($(this).is(':checked')) {
			f.addClass('hover');
			z.removeClass('hover');
		}else{
			z.addClass('hover');
			f.removeClass('hover');
		}
	});

	animation.init();

});



var socket = io.connect();
var animation = {
	_deg : 0,
	angle: 0,
	_stopidx: null,
	init:function(){
		$('.waitDiv').hide();
		
		socket.on('wxreturn', function(res){
			console.log(res.res);
			animation.angle = 0;
		  	animation._deg = 0;

		  	if(sex==0){
		  		animation.angle = Math.sin(3.14159) * res.res * 5 + (72 * res.res) ;
		  	}else{
		  		animation.angle = Math.cos(3.14159) * res.res * 5 - (72 * res.res);
		  	}
		  	console.log((-animation.angle));
			
			animation._stopidx = setInterval(animation.move, 50);
			
		});
		
	},
	getRes: function(){
		var wxn;
		if ($('.choose span[class="hover"]').index()==0) {
			wxn = parseInt($('.nb select').val());
		}else{
			wxn = '';
			$('.yb input').each(function(index, el) {
				wxn += $(this).is(':checked') ? '0' : '1';	
			});
		}
		socket.emit('wx', { fiveele: wxn});
	},
	move: function(){
		$('.waitDiv').show();
		if (sex==0) {
			animation._deg -= 1;
			if (animation._deg < (-animation.angle)) { animation.stop();};
		}else{
			if (animation._deg > (-animation.angle)) { animation.stop();};
			animation._deg += 1;
		}
		$('.main img').css('-webkit-transform', 'rotateZ('+ animation._deg +'deg)');
	},
	stop: function(){
		clearInterval(animation._stopidx);
		$('.waitDiv').hide();
	}
}

